package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.query.DestinationQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") DestinationQueryObject qo) {
        Page<Destination> list = destinationService.query(qo);
        model.addAttribute("page", list);
        List<Destination> toasts = destinationService.queryToast(qo.getParentId());
        model.addAttribute("toasts", toasts);
        return "destination/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Destination destination) {
        if(destination.getId() == null) {
            destinationService.save(destination);
        } else {
            destinationService.update(destination);
        }
        return JsonResult.success();
    }

    @RequestMapping("/updateInfo")
    @ResponseBody
    public Object updateInfo(String id, String info) {
        destinationService.updateInfo(id, info);
        Map map = new HashMap();
        map.put("success", true);
        map.put("msg",null);
        return map;
    }

}
